package Interfaz;

import Controladores.Mesa;
import Controladores.Reservacion;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Adrian
 */
public class Reservaciones extends Ventana_Defaults {

    /**
     * Creates new form Reservaciones
     */
    ResultSet rs;
    String idMesa;
    Reservacion reservacion;
    int idReservacion;
    Mesa mesa;
    int cantidadComensales;
    public Reservaciones(String cantidadComenzales) {
        try {
            initComponents();
            setSize(550, 300);
            setLocationRelativeTo(null);
            setResizable(false);
            reservacion = new Reservacion();
            mesa = new Mesa();
            setTitle("Reservaciones");
            cantidadComensales = Integer.parseInt(cantidadComenzales);
            rs = mesa.mesasDisponibles(cantidadComenzales);
            if(rs.next()){
                idMesa =rs.getString("idMesa");
            }
        } catch (SQLException ex) {
            Logger.getLogger(Reservaciones.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        txtNombre = new javax.swing.JTextField();
        txtTelefono = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setAlwaysOnTop(true);
        getContentPane().setLayout(null);

        jButton1.setFont(new java.awt.Font("AR CENA", 0, 18)); // NOI18N
        jButton1.setText("Reservar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1);
        jButton1.setBounds(382, 201, 120, 33);

        txtNombre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNombreActionPerformed(evt);
            }
        });
        getContentPane().add(txtNombre);
        txtNombre.setBounds(220, 60, 275, 20);
        getContentPane().add(txtTelefono);
        txtTelefono.setBounds(220, 140, 96, 20);

        jLabel1.setFont(new java.awt.Font("AR CENA", 0, 18)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("Nombre:");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(100, 60, 100, 24);

        jLabel3.setFont(new java.awt.Font("AR CENA", 0, 18)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(255, 255, 255));
        jLabel3.setText("Telefono:");
        getContentPane().add(jLabel3);
        jLabel3.setBounds(100, 137, 100, 24);

        jButton2.setFont(new java.awt.Font("AR CENA", 0, 14)); // NOI18N
        jButton2.setText("Atras");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton2);
        jButton2.setBounds(20, 20, 73, 23);

        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/interior.jpg"))); // NOI18N
        getContentPane().add(jLabel4);
        jLabel4.setBounds(0, 0, 580, 300);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtNombreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNombreActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtNombreActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
       
       dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        try {
            rs = reservacion.realizarConsulta("select idreservacion from reservacion");
            rs.afterLast();
            rs.previous();
            System.out.println(rs.getString("idreservacion"));
            idReservacion = rs.getInt("idreservacion");
            reservacion.agregarReservacion(txtNombre.getText(),String.valueOf(cantidadComensales),txtTelefono.getText());
            mesa.actualizarMesa(idMesa,String.valueOf(idReservacion+1),0 );
        } catch (SQLException ex) {
            Logger.getLogger(Reservaciones.class.getName()).log(Level.SEVERE, null, ex);
        }
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField txtNombre;
    private javax.swing.JTextField txtTelefono;
    // End of variables declaration//GEN-END:variables
}
